define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/lihua_company/index' + location.search,
                    add_url: 'general/lihua_company/add',
                    edit_url: 'general/lihua_company/edit',
                    del_url: 'general/lihua_company/del',
                    multi_url: 'general/lihua_company/multi',
                    table: 'lihua_company',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'city_id', title: '城市', searchList: $.getJSON('general/lihua_city/sel'), formatter:
                            function (value, row, index) { return row.lihuacity.name;}},
                        {field: 'store_id', title: '对应门店', searchList: $.getJSON('general/lihua_store/sel'), formatter:
                            function (value, row, index) { return row.lihuastore.name;}},
                        {field: 'avatar', title: '企业logo', operate: false, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name')},
                        {field: 'lng', title: __('Lng'), operate:'BETWEEN'},
                        {field: 'lat', title: __('Lat'), operate:'BETWEEN'},
                        {field: 'address', title: __('Address')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        // {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'tel', title: __('Tel')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $("#selPoint").click(function () {
                var city_id = $("input[name='row[city_id]']").val();
                if (city_id) {
                    Fast.api.open('general/lihua_store/selpoint?city_id='+ city_id, '选取企业坐标', {area:['75%','90%'], shade:0.3});
                } else {
                    Toastr.error('请先选择城市');
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $("#selPoint").click(function () {
                var lng = $("#c-lng").val();
                var lat = $("#c-lat").val();
                Fast.api.open('general/lihua_store/selpoint?lng='+lng+'&lat='+lat, '选取门店坐标', {area:['75%','90%'], shade:0.3});
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});